import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { SysParamService } from '@core/routes-services/sys-param.service';
import { RuleTemplateService } from '@core/routes-services/rule-template.service';

@Component({
  selector: 'app-template-edit',
  templateUrl: './template-edit.component.html',
  styles:[`
    .role-rule{
      border-top: 1px solid #1890ff;
    }
  `]
})
export class TemplateEditComponent implements OnInit {
  type: any = 1;
  entity: any = {};
  validateForm: FormGroup;
  isLoading: boolean = true;

  maxCreditObj: any = {
    'big': 10,
    'small': 8,
    'bag': 6,
  };

  constructor(private fb: FormBuilder,
              private modal: NzModalRef,
              public ruleTemplateService: RuleTemplateService,
              private paramService: SysParamService,
              public message: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.initMaxCreditValue();
    this.initEntity();
  }

  initMaxCreditValue() {
    this.paramService.loadByCodes('OTC_CREDIT_MAX_VALUE_BIG,OTC_CREDIT_MAX_VALUE_SMALL,OTC_CREDIT_MAX_VALUE_BAG').then((res) => {
      console.log(res);
      this.maxCreditObj.big = res.data.OTC_CREDIT_MAX_VALUE_BIG.value;
      this.maxCreditObj.small = res.data.OTC_CREDIT_MAX_VALUE_SMALL.value;
      this.maxCreditObj.bag = res.data.OTC_CREDIT_MAX_VALUE_BAG.value;
    }, err => {
      console.log(err);
    });
  }

  initEntity() {
    this.entity.code = this.entity.code ? this.entity.code : "-1";
    this.ruleTemplateService.detail(this.entity.code).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.entity = res.data;
        this.initValidateForm();
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, err => {
      console.log(err);
      this.message.error('请求出错，请重试');
    });
  }

  initValidateForm() {
    this.validateForm = this.fb.group({
      title: [this.entity.title, [Validators.required, Validators.maxLength(30)]],
      sortNum: [this.entity.sortNum, [Validators.required]],
      type: [this.type, [Validators.required]],

      commonUserBigBoxCredit4: [this.entity.creditRuleVo.commonUser.bigBoxCredit4, [Validators.required]],
      commonUserSmallBoxCredit4: [this.entity.creditRuleVo.commonUser.smallBoxCredit4, [Validators.required]],
      commonUserBagCredit4: [this.entity.creditRuleVo.commonUser.bagCredit4, [Validators.required]],

      promotionSalesBigBoxCredit5: [this.entity.creditRuleVo.promotionSales.bigBoxCredit5, [Validators.required]],
      promotionSalesSmallBoxCredit5: [this.entity.creditRuleVo.promotionSales.smallBoxCredit5, [Validators.required]],
      promotionSalesBagCredit5: [this.entity.creditRuleVo.promotionSales.bagCredit5, [Validators.required]],

      areaUserBigBoxCredit3: [this.entity.creditRuleVo.areaUser.bigBoxCredit3, [Validators.required]],
      areaUserSmallBoxCredit3: [this.entity.creditRuleVo.areaUser.smallBoxCredit3, [Validators.required]],
      areaUserBagCredit3: [this.entity.creditRuleVo.areaUser.bagCredit3, [Validators.required]],

      shopkeeperBigBoxCredit1: [this.entity.creditRuleVo.shopkeeper.bigBoxCredit1, [Validators.required]],
      shopkeeperSmallBoxCredit1: [this.entity.creditRuleVo.shopkeeper.smallBoxCredit1, [Validators.required]],
      shopkeeperBagCredit1: [this.entity.creditRuleVo.shopkeeper.bagCredit1, [Validators.required]],
      shopkeeperBigBoxCredit3: [this.entity.creditRuleVo.shopkeeper.bigBoxCredit3, [Validators.required]],
      shopkeeperSmallBoxCredit3: [this.entity.creditRuleVo.shopkeeper.smallBoxCredit3, [Validators.required]],
      shopkeeperBagCredit3: [this.entity.creditRuleVo.shopkeeper.bagCredit3, [Validators.required]],

      salesclerkBigBoxCredit2: [this.entity.creditRuleVo.salesclerk.bigBoxCredit2, [Validators.required]],
      salesclerkSmallBoxCredit2: [this.entity.creditRuleVo.salesclerk.smallBoxCredit2, [Validators.required]],
      salesclerkBagCredit2: [this.entity.creditRuleVo.salesclerk.bagCredit2, [Validators.required]],
      salesclerkBigBoxCredit1: [this.entity.creditRuleVo.salesclerk.bigBoxCredit1, [Validators.required]],
      salesclerkSmallBoxCredit1: [this.entity.creditRuleVo.salesclerk.smallBoxCredit1, [Validators.required]],
      salesclerkBagCredit1: [this.entity.creditRuleVo.salesclerk.bagCredit1, [Validators.required]],
      salesclerkBigBoxCredit3: [this.entity.creditRuleVo.salesclerk.bigBoxCredit3, [Validators.required]],
      salesclerkSmallBoxCredit3: [this.entity.creditRuleVo.salesclerk.smallBoxCredit3, [Validators.required]],
      salesclerkBagCredit3: [this.entity.creditRuleVo.salesclerk.bagCredit3, [Validators.required]],
    });
  }


  submitForm(): void {
    let that = this;
    if (this.validateForm.invalid) {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    } else {
      that.isLoading = true;
      Object.assign(this.entity, this.validateForm.value);
      this.entity.creditRuleVo.commonUser.bigBoxCredit4 = that.validateForm.value.commonUserBigBoxCredit4;
      this.entity.creditRuleVo.commonUser.smallBoxCredit4 = that.validateForm.value.commonUserSmallBoxCredit4;
      this.entity.creditRuleVo.commonUser.bagCredit4 = that.validateForm.value.commonUserBagCredit4;

      this.entity.creditRuleVo.areaUser.bigBoxCredit3 = that.validateForm.value.areaUserBigBoxCredit3;
      this.entity.creditRuleVo.areaUser.smallBoxCredit3 = that.validateForm.value.areaUserSmallBoxCredit3;
      this.entity.creditRuleVo.areaUser.bagCredit3 = that.validateForm.value.areaUserBagCredit3;

      this.entity.creditRuleVo.promotionSales.bigBoxCredit5 = that.validateForm.value.promotionSalesBigBoxCredit5;
      this.entity.creditRuleVo.promotionSales.smallBoxCredit5 = that.validateForm.value.promotionSalesSmallBoxCredit5;
      this.entity.creditRuleVo.promotionSales.bagCredit5 = that.validateForm.value.promotionSalesBagCredit5;

      this.entity.creditRuleVo.shopkeeper.bigBoxCredit1 = that.validateForm.value.shopkeeperBigBoxCredit1;
      this.entity.creditRuleVo.shopkeeper.smallBoxCredit1 = that.validateForm.value.shopkeeperSmallBoxCredit1;
      this.entity.creditRuleVo.shopkeeper.bagCredit1 = that.validateForm.value.shopkeeperBagCredit1;
      this.entity.creditRuleVo.shopkeeper.bigBoxCredit3 = that.validateForm.value.shopkeeperBigBoxCredit3;
      this.entity.creditRuleVo.shopkeeper.smallBoxCredit3 = that.validateForm.value.shopkeeperSmallBoxCredit3;
      this.entity.creditRuleVo.shopkeeper.bagCredit3 = that.validateForm.value.shopkeeperBagCredit3;


      this.entity.creditRuleVo.salesclerk.bigBoxCredit2 = that.validateForm.value.salesclerkBigBoxCredit2;
      this.entity.creditRuleVo.salesclerk.smallBoxCredit2 = that.validateForm.value.salesclerkSmallBoxCredit2;
      this.entity.creditRuleVo.salesclerk.bagCredit2 = that.validateForm.value.salesclerkBagCredit2;
      this.entity.creditRuleVo.salesclerk.bigBoxCredit1 = that.validateForm.value.salesclerkBigBoxCredit1;
      this.entity.creditRuleVo.salesclerk.smallBoxCredit1 = that.validateForm.value.salesclerkSmallBoxCredit1;
      this.entity.creditRuleVo.salesclerk.bagCredit1 = that.validateForm.value.salesclerkBagCredit1;
      this.entity.creditRuleVo.salesclerk.bigBoxCredit3 = that.validateForm.value.salesclerkBigBoxCredit3;
      this.entity.creditRuleVo.salesclerk.smallBoxCredit3 = that.validateForm.value.salesclerkSmallBoxCredit3;
      this.entity.creditRuleVo.salesclerk.bagCredit3 = that.validateForm.value.salesclerkBagCredit3;

      that.ruleTemplateService.saveOrUpdateEntity(that.entity.code, that.entity).then((res) => {
        if (res.status.toString().startsWith('20')) {
          that.modal.close(true);
        } else {
          that.message.warning(res.message);
        }
        that.isLoading = false;
      }, error => {
        that.message.error(error.message ? error.message : error.error);
        that.isLoading = false;
      });

    }
  }

  close() {
    this.modal.destroy();
  }
}
